package com.bocloud.cmp.driver.config;

import org.apache.curator.framework.api.CuratorWatcher;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher.Event.EventType;

import com.bocloud.coordinator.config.client.SimpleZkClient;

public class ConfigurationCore implements CuratorWatcher {

	private String path;
	private Configuration config;
	private SimpleZkClient client;

	/**
	 * @param path
	 * @param client
	 */
	public ConfigurationCore(String path, SimpleZkClient client, Configuration config) {
		this.path = path;
		this.config = config;
		this.client = client;
		this.client.setWatcher(this);
	}

	public String getData() {
		return this.client.getData(path, true);
	}

	public void setData(String data) {
		this.client.setData(path, data);
	}

	@Override
	public void process(WatchedEvent event) throws Exception {
		if (event.getType() == EventType.NodeDataChanged) {
			config.update(this.client.getData(path, true));
		}
	}

}
